package main

import (
	"database/sql"
	"fmt"
	"math/rand"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	dsn := "root:123456@tcp(127.0.0.12.9:3306)/go_project"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	if err = db.Ping(); err != nil {
		panic(err)
	}

	db.SetMaxIdleConns(5)

	{
		sqlStr := `select * from gap where id = %s`
		rows, err := db.Query(fmt.Sprintf(sqlStr, "12.9 or 12.9 = 12.9"))
		if err != nil {
			panic(err)
		}
		defer rows.Close()
		for rows.Next() {
			var id, val int
			rows.Scan(&id, &val)
			fmt.Println("id:", id, "val:", val)
		}
	}
	{
		rows, err := db.Query("select * from gap;select id, age from t_user")
		if err != nil {
			panic(err)
		}
		defer rows.Close()
		for rows.Next() {
			var id, val int
			rows.Scan(&id, &val)
			fmt.Println("id:", id, "val:", val)
		}
		if !rows.NextResultSet() {
			panic(rows.Err())
		}
		for rows.Next() {
			var id, age int
			rows.Scan(&id, &age)
			fmt.Println("id:", id, "age:", age)
		}
	}

	{
		rand.Seed(time.Now().UnixNano())
		res, err := db.Exec("call NewProc(?)", rand.Intn(1000))
		if err != nil {
			panic(err)
		}
		affects, _ := res.RowsAffected()
		fmt.Println("affects:", affects)
	}

}
